{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6d2faa03-a3cc-449e-a97a-e2723e4fff7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为了减少变量命名复杂性，每个分割线间使用的变量名虽然相同但含义无关。\n",
    "# ————————————————————————————————————————————————————————————————\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8a3e2f20-e271-426a-8bb3-2254f84cfcd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Series和DataFrame对象的理解\n",
    "# Series可以理解为字典，是一列数据，左边是key，右边是value但是允许key重复出现。故Sr属性为index和values\n",
    "# DataFrame可以理解为多列Sr拼接而成，属性为index、columns、values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "beb1b2ee-04c6-4d9d-a7e7-c01199a62260",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    15\n",
       "B    15\n",
       "C    25\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Sr的创建方法：字典法、列表法\n",
    "sr = pd.Series({\n",
    "    'A':15,\n",
    "    'B':15,\n",
    "    'C':25\n",
    "})\n",
    "sr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "11ef786f-0efd-46bb-85a4-e21a62960189",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    15\n",
       "B    16\n",
       "C    26\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = [15,16,26]\n",
    "k = ['A','B','C']\n",
    "sr1 = pd.Series(v,index=k)\n",
    "sr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "48addfcc-5cbb-4030-8799-88683633db68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Index(['A', 'B', 'C'], dtype='object'), array([15, 15, 25], dtype=int64))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Sr的两个属性：index、values\n",
    "sr1.index,sr.values\n",
    "# 可知，Sr和DF的值底层还是使用ndarry存储"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "dadb7415-8244-4f87-8aae-217c5e0a7009",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄\n",
       "A    15    15\n",
       "B    15    16\n",
       "C    25    26"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame的创建方法：字典法、列表法\n",
    "df = pd.DataFrame({\n",
    "    '实岁年龄':sr,\n",
    "    '虚岁年龄':sr1\n",
    "})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "b96abaa0-fc36-4b5b-98fd-c8e5045aa31c",
   "metadata": {},
   "outputs": [],
   "source": [
    "v = [[2,3,5],[1,6,4],[9,6,2]]\n",
    "idx = ['A','B','C']\n",
    "col = ['A1','B1','C1']\n",
    "df1 = pd.DataFrame(v,index=idx,columns=col) # 后两个参数可以省略"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "b00e1891-7cdb-48e7-8fb9-019f64ba27e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A1</th>\n",
       "      <th>B1</th>\n",
       "      <th>C1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A1  B1  C1\n",
       "A   2   3   5\n",
       "B   1   6   4\n",
       "C   9   6   2"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f28138d6-4381-4b24-803c-c2880fbde30c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[2, 3, 5],\n",
       "        [1, 6, 4],\n",
       "        [9, 6, 2]], dtype=int64),\n",
       " Index(['A', 'B', 'C'], dtype='object'),\n",
       " Index(['实岁年龄', '虚岁年龄'], dtype='object'))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DF的三个属性\n",
    "df1.values,df.index,df.columns \n",
    "# 自动类型转换，虽然可能是数值和字符串类型并存，但是统一转换为Object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "324bb79f-4314-4923-8392-f1ba0bbed568",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————\n",
    "# Sr和DF的访问\n",
    "# 分为隐式访问和显式访问，所谓隐是指通过看不到的下标访问，使用iloc\n",
    "# 显是通过标签访问，使用loc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3c24ba3e-f080-4dcb-956b-1b4523eada5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr.iloc[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c1417b55-3253-459b-8039-3a1c69328d3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr.loc['B']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fc591bbe-577f-4527-9dde-0eb47fe4b880",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "B    15\n",
       "C    25\n",
       "dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr.iloc[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8d543e66-a0c2-47bc-a148-1ed6fcbde88c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "C    25\n",
       "B    15\n",
       "A    15\n",
       "dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr.loc[:'A':-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9069e085-07fc-4743-a9c4-eac64f76d19f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄\n",
       "B    15    16"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1:2,0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a568763d-c483-40c7-97b3-beb7c7a49a39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "960947e7-a745-43b3-a378-ece634b40161",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "实岁年龄    15\n",
       "虚岁年龄    15\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ac751f63-3b9f-4bd9-8b35-e3647d0f77ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['A','虚岁年龄']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "73d8bcd0-bb35-47ee-ab11-4ee84da4514a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    15\n",
       "B    15\n",
       "C    25\n",
       "Name: 实岁年龄, dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['实岁年龄'] # columns就是df的key，因此可以简便提取列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "3d747c1a-ece8-426a-a499-42025e927366",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————\n",
    "# Pd中对对象的操作：转置、反转、添加、删除、合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c3db8b59-4f5b-4352-ae50-fba9d55e3835",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄\n",
       "A    15    15\n",
       "B    15    16\n",
       "C    25    26"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8ebcfdf6-4fb8-4748-886d-9fa553e938bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>实岁年龄</th>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>虚岁年龄</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       A   B   C\n",
       "实岁年龄  15  15  25\n",
       "虚岁年龄  15  16  26"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "f84eb036-9a2c-458c-9378-3456e1e84c19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 反转同numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "0e7d9ec2-6f66-4c35-ac0e-b3062df1f280",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加：把sr添加到pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "56d0a774-9189-4e98-afbd-e5ad93fedd63",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['C'] = sr # 添加一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "7c79aec0-7265-4e9b-a1c7-7dcfbdc4c371",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄   C\n",
       "A    15    15  15\n",
       "B    15    16  15\n",
       "C    25    26  25"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1456b7a0-463f-41b5-b627-661d57f99dcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc['D'] = sr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "8af1a5d1-c424-4d01-837d-4231b8528b39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄     C\n",
       "A  15.0  15.0  15.0\n",
       "B  15.0  16.0  15.0\n",
       "C  25.0  26.0  25.0\n",
       "D   NaN   NaN  25.0"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "0ef46363-c8f0-4bc9-892a-687a5fd4aadf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除：使用df.drop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f244233c-2aa1-473c-9092-9068f4574b42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄     C\n",
       "B  15.0  16.0  15.0\n",
       "C  25.0  26.0  25.0"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(index=['A','D']) # 注意添加inplace=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "9ab0018b-8539-4613-98cc-0e87107b0ecc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄     C\n",
       "A  15.0  15.0  15.0\n",
       "B  15.0  16.0  15.0\n",
       "C  25.0  26.0  25.0\n",
       "D   NaN   NaN  25.0"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "f3802891-559c-4e78-9f61-3ccc56658761",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25.0</td>\n",
       "      <td>26.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄\n",
       "A  15.0  15.0\n",
       "B  15.0  16.0\n",
       "C  25.0  26.0\n",
       "D   NaN   NaN"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(columns=['C'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "8f6790ca-0676-4ed5-a2d6-7f1e7379ad15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15.0</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25.0</td>\n",
       "      <td>26.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄\n",
       "B  15.0  16.0\n",
       "C  25.0  26.0"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(index=['A','D'],columns=['C']) # 可以一下子删完"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "743fac65-2077-463d-9610-ae6fe71fbc12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    15\n",
       "B    16\n",
       "C    26\n",
       "A    15\n",
       "B    16\n",
       "C    26\n",
       "dtype: int64"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对象的拼接\n",
    "# sr和sr的拼接\n",
    "sr2 = sr1\n",
    "pd.concat([sr1,sr2]) # 允许重复的key出现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "e465c15f-934f-498e-ab67-72a780fbd91a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A1</th>\n",
       "      <th>B1</th>\n",
       "      <th>C1</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A1  B1  C1  D  E  F\n",
       "A   2   3   5  2  3  5\n",
       "B   1   6   4  1  6  4\n",
       "C   9   6   2  9  6  2"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df1.copy()\n",
    "df2.columns = ['D','E','F']\n",
    "pd.concat([df1,df2],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "8f3e7e5a-cb04-4870-9f24-f10a8f4bede0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   D  E  F\n",
       "A  2  3  5\n",
       "B  1  6  4\n",
       "C  9  6  2"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "e7ed6eef-a582-42cd-9eb6-8f1a19f2c328",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————\n",
    "# 对df进行布尔型选择怎么用。\n",
    "bl = df2.loc[:,'D']>8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "8bf6d754-4af0-4c3c-92fd-33393f100093",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   D  E  F\n",
       "C  9  6  2"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2[bl] # 只能使用布尔型数组选择行哦~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "11530a68-2548-4d4c-aba9-487739c886a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 统计False的值\n",
    "nl = df2.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "e24414be-b104-45be-a43b-42810dbac640",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "D    0\n",
       "E    0\n",
       "F    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(nl,axis=0)  # 统计列用0，统计行用1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "ebf667a9-c021-4473-8caf-aa5348f58ab4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ————————————————————————————————————————————————————————————————\n",
    "# 缺失值的发现与处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "a6608c8e-750e-4fed-b3c6-27580d383e6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    实岁年龄   虚岁年龄      C\n",
       "A  False  False  False\n",
       "B  False   True   True\n",
       "C  False  False  False"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 发现缺失值 df.isnull()\n",
    "df.iloc[1,1] = None\n",
    "df['C'] = ['WYZ',None,'WSH']\n",
    "df.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "2b2acaf3-ecaa-461c-8968-1482423682ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理缺失值：df.dropna():删除缺失的列、删除缺失的行、删除全为nan的行/列\n",
    "# 填充缺失值，df.fillna()常数、该列均值、前值、后值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "dc99a555-9cb1-4c95-91fe-9bf13d30beab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15.0</td>\n",
       "      <td>WYZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26.0</td>\n",
       "      <td>WSH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄    C\n",
       "A    15  15.0  WYZ\n",
       "C    25  26.0  WSH"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "c2fc9bc0-018c-4d3b-b699-8b8eddb96afb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄\n",
       "A    15\n",
       "B    15\n",
       "C    25"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "c906f8af-41d3-4403-b0b0-a44809860661",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15.0</td>\n",
       "      <td>WYZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26.0</td>\n",
       "      <td>WSH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄    C\n",
       "A    15  15.0  WYZ\n",
       "B    15   0.0    0\n",
       "C    25  26.0  WSH"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.fillna(0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "9167031a-4649-4399-88c5-547390c2d5fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15.0</td>\n",
       "      <td>WYZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26.0</td>\n",
       "      <td>WSH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄    C\n",
       "A    15  15.0  WYZ\n",
       "B    15   1.0    1\n",
       "C    25  26.0  WSH"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.fillna(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "38c0b7b8-bc1e-40bb-8a9f-864ca7dc367e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 平均值补充，仅能用于数字列，所以在进行数据处理时先把数字列和Object列分开\n",
    "number_columns = df.select_dtypes(include=[np.number])\n",
    "object_columns = df.select_dtypes(exclude=[np.number])\n",
    "number_mean = number_columns.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "7e7c3182-6407-476d-8e4f-2ad8efadf0e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "number_columns.fillna(number_mean,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "871e87f4-a71b-4b5b-871f-bfe68d9c351b",
   "metadata": {},
   "outputs": [],
   "source": [
    "object_columns.ffill(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "783f515d-d1bd-435d-8b66-28b787ea822d",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_df = pd.concat([number_columns,object_columns],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "717c495c-ef58-4f19-a2e3-ee9f8d6cda18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>15.0</td>\n",
       "      <td>WYZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>15</td>\n",
       "      <td>20.5</td>\n",
       "      <td>WYZ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>25</td>\n",
       "      <td>26.0</td>\n",
       "      <td>WSH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   实岁年龄  虚岁年龄    C\n",
       "A    15  15.0  WYZ\n",
       "B    15  20.5  WYZ\n",
       "C    25  26.0  WSH"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "bff4ece6-7316-4927-8fc7-434397c5f1f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实岁年龄</th>\n",
       "      <th>虚岁年龄</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>18.333333</td>\n",
       "      <td>20.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>5.773503</td>\n",
       "      <td>5.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>15.000000</td>\n",
       "      <td>15.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>15.000000</td>\n",
       "      <td>17.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>15.000000</td>\n",
       "      <td>20.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>20.000000</td>\n",
       "      <td>23.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>25.000000</td>\n",
       "      <td>26.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            实岁年龄   虚岁年龄\n",
       "count   3.000000   3.00\n",
       "mean   18.333333  20.50\n",
       "std     5.773503   5.50\n",
       "min    15.000000  15.00\n",
       "25%    15.000000  17.75\n",
       "50%    15.000000  20.50\n",
       "75%    20.000000  23.25\n",
       "max    25.000000  26.00"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ————————————————————————————————————————————————————————————————\n",
    "final_df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78749c57-4497-4075-a5ea-2e700492c19d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
